package com.generator.myt;
import com.plugin.mybatis.generator.TestDefaultShellCallback135;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GeneratorMain {

    //TODO  定义自己项目模块的目录位置 改成自己的
    private static String TARGET_PROJECT_PREFIX = "D:\\java_workplace\\mingyitang\\yhzj-service\\";

    /**
     * 生成 dao层 模型 mapper
     * @throws Exception
     */
    @Test
    public void generator() throws Exception {
        //项目模块目录 改成自己的
        //String targetProjectPrefix = "D:/java_workplace/jiangmengkeji/xxx-open-server";

        File s = new File("");
        String absolutePath = s.getAbsolutePath();
        //System.out.println("-----获取模块绝对路径absolutePath："+absolutePath);
        //###################################### 设置generatorConfig.xml位置 ##############################
        //.xml位置
        String generatorConfigPath = absolutePath + "/src/main/java/com/generator/myt/generatorConfig.xml";

        //项目 模块目录
        System.setProperty("targetProjectPrefix",TARGET_PROJECT_PREFIX);


        List<String> warnings = new ArrayList<>();
        File configFile = new File(generatorConfigPath);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new TestDefaultShellCallback135(true,false);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

        System.out.println("-----生成完毕！！！");
    }

    /**
     * Vo模型，Service，ServiceImpl，Controller 基础单表操作的生成(CRUD)
     * @throws Exception
     */
    @Test
    public void generatorService() throws Exception{
        File s = new File("");
        String absolutePath = s.getAbsolutePath();
        //System.out.println("-----获取模块绝对路径absolutePath："+absolutePath);
        //###################################### 设置 generatorConfig.xml位置 ##############################
        //.xml位置
        String generatorConfigPath = absolutePath + "/src/main/java/com/generator/能力平台结算模块/generatorControllerConfig.xml";

        //项目模块目录 改成自己的
        //String targetProjectPrefix ="D:/java_workplace/jiangmengkeji/iot-open-server/";

        //如果要生成 Service  模块目录 如果不需要代码注释
        System.setProperty("targetProjectPrefix",TARGET_PROJECT_PREFIX);
        //如果要生成 ServiceImpl 模块目录 如果不需要代码注释
        System.setProperty("implTargetProjectPrefix",TARGET_PROJECT_PREFIX);
        //如果要生成 controller 模块目录 如果不需要代码注释
        System.setProperty("controllerTargetProjectPrefix",TARGET_PROJECT_PREFIX);


        List<String> warnings = new ArrayList<>();
        File configFile = new File(generatorConfigPath);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new TestDefaultShellCallback135(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        System.out.println("-----生成完毕！");
    }
}
